package org.cjstudio.messageboard;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cjstudio.messageboard.MessageCommands.MessageCommand;
import org.cjstudio.messageboard.model.Message;

@SuppressWarnings("serial")
public class MessageServlet extends HttpServlet {
	
	public static final String pn_name = Message.pn_name;
	public static final String pn_phoneNumber = Message.pn_phoneNumber;
	public static final String pn_content = Message.pn_content;
	
	// Create New Message Entry
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String phoneNumber = phoneNumberFromRequestParameters(req);
		Message message = messgeFromRequestParameters(req);

		@SuppressWarnings("rawtypes")
		MessageCommand messageCommand = MessageCommands.getCommand(MessageCommands.OPCODE_CREATE);
		messageCommand.setParameters(new MessageCommands.Parameters(phoneNumber, message));
		
		if(messageCommand.getReturyType() == Boolean.class) {
			if ((Boolean) messageCommand.execute()) {
				// operation success
				resp.setStatus(HttpServletResponse.SC_OK);
			} else {
				// already there
				// apply overwrite policy
				resp.setStatus(HttpServletResponse.SC_OK);
			}
		}
	}

	// Retrieve My Message
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String phoneNumber = phoneNumberFromRequestParameters(req);

		@SuppressWarnings("rawtypes")
		MessageCommand messageCommand = MessageCommands.getCommand(MessageCommands.OPCODE_RETRIEVE);
		messageCommand.setParameters(new MessageCommands.Parameters(phoneNumber));
		
		if(messageCommand.getReturyType() == Message.class) {
			Message message = (Message) messageCommand.execute();
			if (message != null) {
				resp.setStatus(HttpServletResponse.SC_OK);
	
				resp.setContentType("application/json");
				resp.setCharacterEncoding("utf-8");
	
				resp.getWriter().print("[\"" + message.getContent() + "\"]");
			} else {
				// message not found
				resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
			}
		}
	}
	
	// Update My Message
	public void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String phoneNumber = phoneNumberFromRequestParameters(req);
		Message newMessage = messgeFromRequestParameters(req);

		@SuppressWarnings("rawtypes")
		MessageCommand messageCommand = MessageCommands.getCommand(MessageCommands.OPCODE_UPDATE);
		messageCommand.setParameters(new MessageCommands.Parameters(phoneNumber, newMessage));
		
		if(messageCommand.getReturyType() == Boolean.class) {
			if ((Boolean) messageCommand.execute()) {
				// operation success
				resp.setStatus(HttpServletResponse.SC_OK);
			} else {
				// message not found
				resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
			}
		}
	}

	// Delete My Message
	public void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String phoneNumber = phoneNumberFromRequestParameters(req);

		@SuppressWarnings("rawtypes")
		MessageCommand messageCommand = MessageCommands.getCommand(MessageCommands.OPCODE_DELETE);
		messageCommand.setParameters(new MessageCommands.Parameters(phoneNumber));

		if(messageCommand.getReturyType() == Boolean.class) {
			if ((Boolean) messageCommand.execute()) {
				resp.setStatus(HttpServletResponse.SC_OK);
			} else {
				// message not found
				resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
			}
		}
	}
	
	private String phoneNumberFromRequestParameters(HttpServletRequest req) {
		return req.getParameter("phoneNumber");
	}

	private Message messgeFromRequestParameters(HttpServletRequest req) {
		String name = req.getParameter("name");
		String phoneNumber = req.getParameter("phoneNumber");
		String content = req.getParameter("content");
		return new Message(name, phoneNumber, content);
	}
}